﻿using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections.Generic;
using MyClever.Lib;

namespace MyCleverMissionEditor
{
    /// <summary>
    /// hält alle Slots zusammen und gibt alle zu zeichnenden Rectangles aus
    /// </summary>
    public class SlotCollection : CollectionBase
    {
        private PackageSheet parentControl;

        public SlotCollection(PackageSheet parentControl)
        {
            this.parentControl = parentControl;

            int posX = 0;
            foreach (DataGridViewColumn columnOn in this.parentControl.DataGridView.Columns)
            {
                MissionPluginPackageColumn test = (MissionPluginPackageColumn)this.parentControl.PluginPackage.Columns[columnOn.Name];

                Size cellHeaderSize = columnOn.HeaderCell.Size;

                int offset = 10 + (10 * columnOn.DisplayIndex);
                Slot inputSlot = new Slot(Slot.DirectionType.Input, new Rectangle(new Point(-30, this.parentControl.DataGridView.Location.Y - 4 - offset), new Size(posX + cellHeaderSize.Width / 2 + 30, offset)), test.HasColumnReference);
                this.List.Add(inputSlot);
                this.parentControl.Controls.Add(inputSlot);

                Slot outputSlot = new Slot(Slot.DirectionType.Output, new Rectangle(new Point(posX + cellHeaderSize.Width / 2, this.parentControl.DataGridView.Location.Y + this.parentControl.DataGridView.Height), new Size(this.parentControl.Width, this.parentControl.DataGridView.Columns.Count * 10 - (10 * columnOn.DisplayIndex))), false);
                this.List.Add(outputSlot);
                this.parentControl.Controls.Add(outputSlot);

                posX += cellHeaderSize.Width;
            }
        }

        /// <summary>
        /// We must change the output slot location, every time we collapse\expand the packageSheet
        /// </summary>
        public void OnUpdate(object sender, System.EventArgs e)
        {
            foreach (Slot slotOn in this.List)
            {
                if (Slot.DirectionType.Output == slotOn.SlotType)
                {
                    slotOn.ArrowRectangle = new Rectangle(new Point(slotOn.ArrowRectangle.Location.X, this.parentControl.DataGridView.Location.Y + this.parentControl.DataGridView.Height), slotOn.ArrowRectangle.Size);
                }
            }
        }

        public List<Point[]> CalcLinesToDraw(PluginSheet pluginSheet)
        {
            List<Point[]> lines = new List<Point[]>();
            foreach (Slot slotOn in this.List)
            {
                if (true == slotOn.IsActive)
                {
                    Point[] clientLines = new Point[3];
                    for (int i = 0; i < 3; i++)
                    {
                        clientLines[i] = pluginSheet.PointToClient(this.parentControl.PointToScreen(slotOn.Lines[i]));   
                    }
                    lines.Add(clientLines);
                }
            }
            return lines;
        }
    }
}
